package com.ocom.human.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.master.EntityMngUserForm;
import com.ocom.common.entity.master.EntityMngUserInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.human.dao.IDaoMngUserForm;
import com.ocom.human.service.MngUserFormService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MngUserFormServiceImpl extends ServiceImpl<IDaoMngUserForm, EntityMngUserForm> implements MngUserFormService {

    @Autowired
    private IDaoMngUserForm iDaoMngUserForm;

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public EntityMngUserForm findByUserIdAndPageAndType(Long userId, String page, Integer type) {
        QueryWrapper<EntityMngUserForm> queryWrapper = Wrappers.query();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("page", page);
        queryWrapper.eq("type", type);
        queryWrapper.orderByDesc("update_time");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean deleteByUserIdAndPageAndType(Long userId, String page, Integer type) {
        QueryWrapper<EntityMngUserForm> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("page", page);
        wrapper.eq("type", type);
        return iDaoMngUserForm.delete(wrapper) > 0 ? true : false;
    }
}
